﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CDSP
{
    public class FAskQuestionsWP : Portal.Interface.WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.FAskedQuestions";
            }
        }

        public override bool IsMainWebPart
        {
            get
            {
                return true;
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Hỏi Đáp");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Description", "");
            }
        }


        public override WebPartInt[] SubWebParts
        {
            get
            {
                WebPartInt[] SubWebParts = new WebPartInt[]{
                    new UpdateFAskQuestionsWP()
                };
                return SubWebParts;
            }
        }

        public string GetViewFAQUrl(string belongSite, string Subject, string ShortId)
        {
            Subject = Environments.ConvertSubject(Subject);
            string Url = Environments.FrontHttpRoot + "/" + Environments.AppendAlias + "-" + belongSite + "/" + Subject + "-vfa-" + ShortId + "." + Environments.Extension;
            if (Environments.MultiLanguageInSite == 1)
            {
                Url = Url + "?lang=" + Environments.ActiveLanguage;
            }
            return Url;
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                string Url = Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
                return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang đọc dữ liệu...") + "</a>";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSidePublishing(string AskID)
        {
            try
            {
                return ResourceBLL.GetText(WebPartId, "PublishingOk", "Xuất bản lên website thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetShowNewsItemForm(string AskID)
        {
            string UpdateUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId","News"),
                        new ParamCls("AskID",AskID)
                    });
            return UpdateUrl;
        }


        [AjaxPro.AjaxMethod]
        public string GetShowUpdateForm(string AskID)
        {
            string UpdateUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UpdateFAskQuestionsWP().WebPartId),
                        new ParamCls("AskID",AskID)
                    });
            return UpdateUrl;
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmPublishingMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmPublishingMsg", "Bạn đã chắc chắn phát hành lên website public chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmDeleteMsg", "Bạn đã chắc chắn xóa chưa?");
        }


        [AjaxPro.AjaxMethod()]
        public string GetHtml(string belongSite, int CurrentPageIndex, int status)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                Environments.CheckWebPartAllow(WebPartId, true);
                string RetHtml = string.Empty;
                DataTable dtFAQ = new FAskQuestionsBLL().ReadingForAdmin(status);
                int ItemPerPages = 20;
                int MaxPage = dtFAQ.Rows.Count / ItemPerPages;
                if (dtFAQ.Rows.Count % ItemPerPages != 0) MaxPage++;
                int StartIndex = CurrentPageIndex * ItemPerPages;
                int EndIndex = StartIndex + ItemPerPages;
                if (EndIndex > dtFAQ.Rows.Count)
                {
                    EndIndex = dtFAQ.Rows.Count;
                }
                string SelectPageText = "";
                if (MaxPage > 1)
                {
                    SelectPageText = ResourceBLL.GetText(WebPartId, "SelectPage", "Trang") + ": <select id=divSelectPageLawDocumentType onchange=javascript:JumPageLawDocumentType();>";
                    for (int iIndex = 0; iIndex < MaxPage; iIndex++)
                    {
                        if (CurrentPageIndex == iIndex)
                        {
                            SelectPageText += "<option selected >" + (iIndex + 1) + "</option>";
                        }
                        else
                        {
                            SelectPageText += "<option>" + (iIndex + 1) + "</option>";
                        }
                    }
                    SelectPageText += "</select>";
                }
                RetHtml +=
                    "<div id=divProcessing></div>" +

                    "   <table>" +
                    "       <tr>" +
                    "           <th>#</th>" +
                    "           <th>" + ResourceBLL.GetText(WebPartId, "AskContent", "Câu hỏi") + "</th>" +
                    "           <th>" + ResourceBLL.GetText(WebPartId, "AskBy", "Người gửi") + "</th>" +
                    "           <th>" + ResourceBLL.GetText(WebPartId, "AskTime", "Ngày gửi") + "</th>" +
                    "           <th>" + ResourceBLL.GetText(WebPartId, "AskStatus", "Trạng thái") + "</th>" +
                    "           <th>" + ResourceBLL.GetText(WebPartId, "AskTotalClick ", "Lượt xem") + "</th>" +
                    "           <th>Chức năng</th>" +
                    "           <th></th>" +
                    "       </tr>";
                for (int iIndex = StartIndex; iIndex < EndIndex; iIndex++)
                {
                    string AskID = (string)dtFAQ.Rows[iIndex]["AskID"];
                    string AskContent = "";
                    try
                    {
                        AskContent = (string)dtFAQ.Rows[iIndex]["AskContent"];
                    }
                    catch { }
                    string AskBy = "";
                    try
                    {
                        AskBy = (string)dtFAQ.Rows[iIndex]["AskBy"];
                    }
                    catch { }
                    string AskEmail = "";
                    try
                    {
                        AskEmail = (string)dtFAQ.Rows[iIndex]["AskEmail"];
                    }
                    catch { }
                    DateTime Time = DateTime.Now;
                    try
                    {
                        Time = (DateTime)dtFAQ.Rows[iIndex]["AskTime"];
                    }
                    catch { }

                    int TotalClick = 0;
                    try
                    {
                        TotalClick = (int)dtFAQ.Rows[iIndex]["AskTotalClick"];
                    }
                    catch { }

                    string AskAnswer = "";
                    try
                    {
                        AskAnswer = (string)dtFAQ.Rows[iIndex]["AskAnswer"];
                    }
                    catch { }

                    string AskStatus = "Ẩn";
                    try
                    {
                        if ((bool)dtFAQ.Rows[iIndex]["AskStatus"])
                            AskStatus = "Hiện";
                    }
                    catch { }

                    string DeleteUrl = "javascript:ClientDeleteFAQ('" + AskID + "')";


                    RetHtml +=
                    "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                    "           <td>" + (iIndex + 1) + "</td>" +
                    "           <td class=cssTdSubjectOnlineSupport>" + AskContent + "</td>" +
                    "           <td class=cssTdSubjectOnlineSupport>" + AskBy + "</td>" +
                    "           <td class=cssTdSubjectOnlineSupport>" + Time + "</td>" +
                    "           <td class=cssTdSubjectOnlineSupport>" + AskStatus + "</td>" +
                    "           <td class=cssTdSubjectOnlineSupport>" + TotalClick + "</td>" +
                    "           <td><a href=\"javascript:ShowFormClientUpdateUrl('" + AskID + "');\">" + ResourceBLL.GetText(WebPartId, "QA_Update", "Cập nhật") + "</a></td>" +
                    "           <td><a href=\"" + DeleteUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Delete", "Xóa") + "</a></td>" +
                    "       </tr>";
                }
                RetHtml +=
                    "   </table>";
                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.CDSP.FAskQuestionsWP));
            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CDSP", "QuestionsWP.js") +
                Portal.Environment.WebPartUtilities.AttachStyleSheet("Portal.CDSP", "StyleSheet.css") +
                "<div id = status>" +
                ResourceBLL.GetText(WebPartId, "SelectStatus", "Trạng thái:") +
                "<select id=divSelectFilter  onchange=\"javascript:JumPageStatus('vie');\">" +
                "<option value=1>Đã xuất bản</option>" +
                "<option value=0>Chưa xuất bản</option>" +
                "</select></div>" +
                "<div id = ContentSearchResult>" +
                    GetHtml("vie", 0, 1) +
                "</div>";
            return RetHtml;
        }



        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteFAQ(string AskID)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);

                SqlTransaction sqlTransaction = Environments.CreateTransaction();
                try
                {
                    new FAskQuestionsBLL().ServerSideDeleteFAQ(sqlTransaction, AskID);
                    sqlTransaction.Commit();
                }
                catch (Exception ex)
                {
                    sqlTransaction.Rollback();
                    throw (ex);
                }
                return ResourceBLL.GetText(WebPartId, "DeleteOkMessage", "Xóa thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        public override bool AllowFrontService
        {
            get
            {
                return false;
            }
        }




    }
}
